/* 牌谱屋的对局列表获取脚本,代码草稿纸中使用,地址如:https://amae-koromo.sapk.ch/player/70241917/16 */
(function(){

/*** Starts here ***/

/** Functions **/

/* 从一个记录一场牌局的div中获取信息,包括牌谱链接link,日期pdate,参与者players(按得分排序) */
function getDataFromDiv(div){
  
  var result = {};
  var texts = div.innerText.split("\n");
  
  result['link'] = div.getElementsByTagName("a")[0]["href"];
  result['pdate'] = texts[texts.length-1];
  result['players'] = [texts[2], texts[3], texts[4], texts[5]]
  
  //正则找分数然后排序的函数
  function scoreCmp(text1, text2){
    var patt = /\[(-?\d+)\]/i;
    var r = 0.0;
    try{
      r = 1.0*text2.match(patt)[1]-1.0*text1.match(patt)[1];
    }catch(err){
      r= 0.0;
    }
    return r;
  }
  
  result['players'].sort(scoreCmp)
  return result;
}

/* get all row data from row list div */
function getAllData(){
  var result = [];
  var topDiv = document.getElementsByClassName("ReactVirtualized__Grid__innerScrollContainer")[0];
  var childDivList = topDiv.childNodes
  for(var i = 0;i < childDivList.length;i++){
    var div = childDivList[i];
    result.push(getDataFromDiv(div));
  }
  return result;
}

/** Main Script **/
/* 把获取的资料整理成文字 */
var text = "";
var data = getAllData();
for(var i = 0;i < data.length;i++){
  text += "===== "+" 牌谱"+(i+1)+" "+data[i]["pdate"]+"\n";
  text += "link : "+data[i]["link"]+"\n";
  for(var j = 0;j < 4;j++){
    text += data[i]["players"][j]+"\n";
  }
}
return text;

/*** Ends here ***/

})();